package com.example.ytyproject.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.ytyproject.entity.Role;
import com.example.ytyproject.entity.User;
import com.example.ytyproject.entity.vo.R;
import com.example.ytyproject.service.UserService;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yg
 * @since 2023-10-27
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    @PostMapping("/listByPage")
    public R listByPage(@RequestBody User user) {
        return userService.listByPage(user);
    }
    @DeleteMapping("/deleteData")
    public R deleteData(@Param("id") String id) {
        userService.removeById(id);
        return R.ok();
    }
    @PostMapping("/addUser")
    public R addUser(@RequestBody User user) {
        log.info("addUser param: {}", JSONObject.toJSONString(user));
        userService.addUser(user);
        return R.ok();
    }

    @PostMapping("/updateUser")
    public R updateUser(@RequestBody User user) {
        log.info("updateUser param: {}", JSONObject.toJSONString(user));
        userService.updateUser(user);
        return R.ok();
    }

    @ApiModelProperty("角色分配")
    @PostMapping("/roleAllocation")
    public R roleAllocation(@RequestBody List<Role> roles) {
        log.info("updateUser param: {}", JSONObject.toJSONString(roles));
        userService.roleAllocation(roles);
        return R.ok();
    }

    @GetMapping("/role")
    public String role() {
        return"超级管理员角色";
    }

    @GetMapping("/permission")
    public String permission() {
        return"新增或编辑权限";
    }
}
